﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.Modules;
using Ruitu.Edu.TrainingDemo.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ruitu.Edu.TrainingDemo.ViewModels
{
    public class PersonPartViewModel : IValidatableObject
    {
        [Required]
        public string Name { get; set; }

        [Required]
        public Handedness Handedness { get; set; }

        [Required]
        public DateTime? BirthDateUtc { get; set; }

        [BindNever]

        public PersonPart PersonPart { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var localizer = validationContext.GetService<IStringLocalizer<PersonPartViewModel>>();
            var clock = validationContext.GetService<IClock>();

            if (BirthDateUtc.HasValue && clock.UtcNow < BirthDateUtc.Value.AddYears(18))
            {
                yield return new ValidationResult(
                    localizer["出生日期不能晚于当前时间。"],
                    new[] { nameof(BirthDateUtc) });
            }
        }
    }
}
